var util = require("../../utils/util")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalSale:0,//业绩
    totalAmount:0,
    list:[{
      "id": 1000,
      "moths": 4,
      "productNum": 3,
      "sale": 111,
      "salesmanCode": "string",
      "salesmanName": "string",
      "years": 2018
    },{
      "id": 1000,
      "moths": 4,
      "productNum": 3,
      "sale": 111,
      "salesmanCode": "string",
      "salesmanName": "string",
      "years": 2018
    }],
    show:[false,false],//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData_a:[],// 年
    selectData_b:[...Array(12).keys()],// 0~11 月
    index:[0,null]
  },
  // 点击下拉显示框
  selectTap(e){
    let select = e.currentTarget.dataset.i
    console.log(select)
    var toDo = "show["+select+"]";
    this.setData({
      [toDo]: !this.data.show[select]
    });
  },
  // 点击下拉列表
  optionTap(e){
    let select = e.currentTarget.dataset.select//第几个选择器 0，1
    let index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    console.log("第"+select+"个selector,第"+index+"个option")
    this.setData({
      ['index['+select+']']:index,
      ['show['+select+']']:!this.data.show[select]
    });
  },
  bindSearch:function(){
    let that = this
    if (that.data.index[1])
      util.post({
        url:"https://groomtea.icebartech.com/api/admin/performance/performancePage",
        data:{
          "month": that.data.index[1]+1,
          "pageIndex": 0,
          "pageSize": 10,
          "salesmanCode": app.globalData.userInfo.userId,
          "salesmanName": app.globalData.userInfo.nickname
        },
        success:(res)=>{
          app.titleToast(res.msg)
          console.log(res.data.bussData)
          that.setData({
            list:res.data.bussData
          })
        },
        complete:()=>{
          console.log("load complete")
          that.computeData()
        }
      });
    else app.titleToast("请选择月份");
  },
  computeData:function(){
    let that = this
    let amount = 0;
    let sale = 0;
    that.data.list.forEach(element => {
      amount += element.productNum
      sale += element.sale
    });
    that.setData({
      totalAmount:amount,
      totalSale:sale
    })
  },
  onShow: function () {
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y =date.getFullYear()+0;
    this.setData({selectData_a:[Y+"-"+(Y-1999)]})//暂时设置本年
    this.computeData()
    // let that = this
    // util.post({
    //   url:"https://groomtea.icebartech.com/api/admin/performance/performancePage",
    //   data:{
    //     "month": 1,
    //     "pageIndex": 0,
    //     "pageSize": 10,
    //     "salesmanCode": 7,
    //     "salesmanName": "李四"
    //   },
    //   success:(res)=>{
    //     app.titleToast(res.msg)
    //     console.log(res.data.bussData)
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})